package com.huayi.datagovernance.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.codec.binary.Base64;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class AwayTools {

        /**
         * 对象取整
         *
         * @param obj
         * @return
         */
        public static int intValue(Object obj) {
                if (obj == null) {
                        return 0;
                }
                if (obj instanceof Integer) {
                        return ((Integer) obj).intValue();
                } else if (obj instanceof Float) {
                        return Math.round(((Float) obj).floatValue());
                } else if (obj instanceof Double) {
                        return Math.round(((Double) obj).floatValue());
                } else if (obj instanceof BigDecimal) {
                        return Math.round(((BigDecimal) obj).floatValue());
                } else {
                        try {
                                return Integer.parseInt(toString(obj));
                        } catch (Exception e) {
                                return 0;
                        }
                }
        }

        /**
         * 对象取浮点
         *
         * @param obj
         * @return
         */
        public static float floatValue(Object obj) {
                if (obj == null) {
                        return 0;
                }
                if (obj instanceof Integer) {
                        return ((Integer) obj).intValue();
                } else if (obj instanceof Float) {
                        return ((Float) obj).floatValue();
                } else if (obj instanceof Double) {
                        return ((Double) obj).floatValue();
                } else if (obj instanceof BigDecimal) {
                        return ((BigDecimal) obj).floatValue();
                } else {
                        try {
                                return Float.parseFloat(toString(obj));
                        } catch (Exception e) {
                                return 0;
                        }
                }
        }

        /**
         * 对象取Long
         *
         * @param obj
         * @return
         */
        public static long longValue(Object obj) {
                if (obj == null) {
                        return 0;
                }
                if (obj instanceof Long) {
                        return ((Long) obj).longValue();
                } else if (obj instanceof Integer) {
                        return ((Integer) obj).longValue();
                } else if (obj instanceof Float) {
                        return Math.round(((Float) obj).floatValue());
                } else if (obj instanceof Double) {
                        return Math.round(((Double) obj).floatValue());
                } else if (obj instanceof BigDecimal) {
                        return Math.round(((BigDecimal) obj).floatValue());
                } else {
                        try {
                                return Long.parseLong(toString(obj));
                        } catch (Exception e) {
                                return 0;
                        }
                }
        }

        /**
         * 对象取double
         *
         * @param obj
         * @return
         */
        public static double doubleValue(Object obj) {
                if (obj == null) {
                        return 0l;
                }
                String temp = obj.toString().trim();
                if (StringUtils.isEmpty(temp)) {
                        return 0l;
                }

                try {
                        double num = Double.parseDouble(temp);
                        return num;
                } catch (Exception e) {
                        return 0l;
                }
        }

        public static String toString(Object str) {
                if (str == null) {
                        return "";
                } else {
                        try {
                                return str.toString().trim();
                        } catch (Exception e) {
                                return "";
                        }
                }
        }

        public static String checkNullString(String str) {
                if (str == null) {
                        return "";
                } else {
                        return str.trim();
                }
        }

        public static String ToString(Object str) {
                if (str == null) {
                        return "";
                } else {
                        try {
                                return str.toString().trim();
                        } catch (Exception e) {
                                return "";
                        }
                }
        }

        /**
         * 批次号生成
         *  生成规则：当天日期[8位]+序列号[3至24位]，如：202002240000001
         *
         * @return
         */
        public static String randomBatchNum() {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
                String format = dateFormat.format(new Date());
                int max = 24;
                int min = 3;
                Random random = new Random();
                int s = random.nextInt(max) % (max - min + 1) + min;
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < s; i++) {
                        Integer val = (int) (Math.random() * 9 + 1);
                        buffer.append(val.toString());
                }
                return format + buffer.toString();
        }

        /**
         * 根据图片路径得到图片的二进制数据，并转base64
         *
         * @param path
         * @return
         * @throws Exception
         */
        public static String getArrayByUrl(String path) throws Exception {
                // 输入流
                InputStream inStream = new BufferedInputStream(new FileInputStream(path));

                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                //创建一个Buffer字符串
                byte[] buffer = new byte[1024];
                //每次读取的字符串长度，如果为-1，代表全部读取完毕
                int len = 0;
                //使用一个输入流从buffer里把数据读取出来
                while ((len = inStream.read(buffer)) != -1) {
                        //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
                        outStream.write(buffer, 0, len);
                }
                //关闭输入流
                inStream.close();
                //把outStream里的数据写入内存
                byte[] data = outStream.toByteArray();

                //byte[]转base64
                return Base64.encodeBase64String(data);
        }

//        /**
//         * 处理文件或图片
//         *
//         * @param attachmentList
//         * @return
//         * @throws Exception
//         */
//        public static List<AttachmentEntity> getFileList(List<AttachmentEntity> attachmentList) throws Exception {
//                if (attachmentList != null && attachmentList.size() > 0) {
//                        for (int i = 0; i < attachmentList.size(); i++) {
//                                attachmentList.get(i).setArrayurl(AwayTools.getArrayByUrl(attachmentList.get(i).getFilepath() + attachmentList.get(i).getFilealias()));
//                        }
//                }
//                return attachmentList;
//        }

        /**
         * 获取32位UUId
         * @return
         */
        public static String getUUID42(){
            UUID uuid = UUID.randomUUID();
            return "SBID000000"+uuid.toString().toUpperCase().replaceAll("-","");
        }

        /**
         * 获取随机数
         * @return
         */
        public static long getRandom(){
                long count = (int)(Math.random()*(99999-10000+1)+10000);
                return count;
        }
}
